import Vue from 'vue'
import VueRouter from 'vue-router'
import ElementUI from 'element-ui'
import Storage from 'vue-ls'
import NProgress from 'nprogress'
import store from '@/store'
import routes from './routers'
import { title } from '@/settings.js'
import 'element-ui/lib/theme-chalk/index.css'
import 'nprogress/nprogress.css'
const options = {
    namespace: '',
    name: 'ls',
    storage: 'session',
};
NProgress.configure({ showSpinner: false });
Vue.use(VueRouter);
Vue.use(ElementUI);
Vue.use(Storage, options);
Vue.prototype.$progress = NProgress;

const router = new VueRouter({
    scrollBehavior: () => ({ y: 0 }),
    routes
});
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
    return originalPush.call(this, location).catch(err => err)
}
// 默认 true
let hasAccess = (userInfo, to) => {
    // true开启权限验证模式 ，false 不使用权限验证
    if (!userInfo.menus && (!userInfo.isAuth || userInfo.isAuth === false)) return true;
    if (!to.meta) return true;
    return true;
};

router.beforeEach((to, from, next) => {
    NProgress.start();
    document.title = (to.meta && to.meta.title ? (to.meta.title + ' - ') : '') + title;
    var isLogin = Vue.ls.get('userInfo');
    if (to.path === '/login' || to.path === '/register') {
        next();
    } else {
        if (isLogin) {
            if (!store.getters.isAuthToken) {
                store.dispatch('concatRlues', isLogin).then(routes => {
                    router.matcher = new VueRouter({ scrollBehavior: () => ({ y: 0 }) }).matcher;
                    router.addRoutes(routes);
                    next({ ...to, replace: true });
                }).catch(err => {
                    Vue.ls.remove('userInfo');
                    next('/login');
                });
            } else {
                if (to.path === '/401' || to.path === '/404') {
                    next();
                } else {
                    if (hasAccess(store.getters.userInfo, to)) {
                        next();
                    } else {
                        if (to.path === '/404') {
                            next({ path: '/404', replace: true });
                        } else if (to.path !== '/401') {
                            ElementUI.Message.error({ message: '没有权限', center: true });
                            next({ path: '/401', replace: true });
                        }
                        NProgress.done();
                    }
                }
            }
        } else {
            ElementUI.Message.error({ message: '请先登录', center: true });
            NProgress.done();
            next('/login');
        }
    }
});
router.afterEach((to, from) => {
    if ((to.matched[1] && !to.matched[1].components.default) || to.path === '*') {
        console.log('%c ! router components not exists', 'background:#52bea6;color:#fff');
        router.push({ path: '/404', replace: true });
    }
    NProgress.done();
});
router.onError(error => {
    if (error.message && error.message.includes('Cannot') && error.message.includes('find') && error.message.includes('module')) {
        console.log('%c ! router components not exists', 'background:#52bea6;color:#fff', error);
        router.push({ path: '/404', replace: true });
    } else {
        console.log('%c ! router error', 'background:#000;color:#bada55', error);
    }
    NProgress.done();
});
export default router;